package main

import (
	"fmt"
	"log"
	"time"

	opio "gitee.com/haodreams/godriver/opdb/opio/driver"
)

func testSub(host string, port int, timeout int, user string, password string) {
	op, err := opio.Init(host, port, 60, user, password)
	if err != nil {
		log.Fatal("init conn error:", err)
		return
	}
	op.SetCompressModel(opio.ZIP_MODEL_Frame)
	sub, err := op.NewSubscribe("Realtime")

	if err != nil {
		fmt.Println("error", err)
		return
	}

	_ = op.Close()

	names := []string{"ED2.DFB1Q.YNXNY_DFB_1_027_WSPDMIND"}
	sub.SetSnapshot(true)
	_ = sub.InitSubscribe(names, "GN", func(res *opio.Response) {
		errno := res.GetErrNo()
		if errno == 0 {
			log.Println("=================")
			format(res.GetDataSet())
		} else {
			fmt.Println("error", errno)
		}
	})

	//fmt.Println("add 1")
	//_ = sub.Subscribe(ids2)
	time.Sleep(30 * time.Second)

	fmt.Println("remove ~~")
	//_ = sub.UnSubscribe(ids2)
	time.Sleep(10 * time.Minute)
	sub.Close()
}

func testInsertSQL(host string, port int, timeout int, user string, password string) {
	op, err := opio.Init(host, port, 60, user, password)
	if err != nil {
		log.Fatal("init conn error:", err)
		return
	}
	//_ = op.SetCompressModel(opio.ZIP_MODEL_Frame)
	//sql := "insert into Realtime (GN,AV) values('AE1.BLG.MX_BLG_12_YK005',100)"
	sql := `select AV,TM from DD1.Archive
	where GN = 'DD1.NS.GD_NS_3_DQ001'
	and tm between 1603210500 and 1603469700
	and mode = 'raw'
	order by TM asc	`
	req := op.NewRequest(nil)
	req.SetID(1)
	req.SetService("openplant")
	req.SetAction(opio.ActionExecSQL)
	req.SetSQL(sql)
	_ = req.WriteAndFlush()

	res, err := req.GetResponse()
	if err != nil {
		log.Fatal("get response error:", err)
	}
	rs := res.GetDataSet()
	format(rs)
	rs.Close()
	_ = op.Close()
}

func testInsertGN(host string, port int, timeout int, user string, password string) {

	op, err := opio.Init(host, port, 60, user, password)
	if err != nil {
		fmt.Println(err)
		return
	}

	req := op.NewRequest(nil)
	req.SetID(1)
	req.SetService("openplant")
	req.SetAction(opio.ActionInsert)

	table := opio.NewTable("Realtime", 0)
	table.AddColumn("GN", opio.VtString, 0)
	table.AddColumn("AV", opio.VtDouble, 0)
	table.AddColumn("DS", opio.VtInt16, 0)
	table.AddColumn("TM", opio.VtDouble, 0)

	start := time.Now()
	err = table.SetColumnString(0, "HNJT.CALC.O_RUNNUM")
	if err != nil {
		fmt.Println(err)
		return
	}
	err = table.SetColumnDouble(1, 4.0)
	if err != nil {
		fmt.Println(err)
		return
	}
	err = table.SetColumnInt16(2, 1, 0)
	if err != nil {
		fmt.Println(err)
		return
	}

	err = table.SetColumnDouble(3, float64(time.Now().Unix()))
	if err != nil {
		fmt.Println(err)
		return
	}
	table.BindRow()

	_ = req.SetTable(table)
	fmt.Println("construct request use time:", time.Since(start))
	start = time.Now()
	err = req.Write()
	if err != nil {
		fmt.Println(err)
		return
	}
	err = req.WriteContent(table)
	if err != nil {
		fmt.Println(err)
		return
	}

	req.Flush()
	fmt.Println("write use time:", time.Since(start))
	start = time.Now()
	res, err := req.GetResponse()
	if err != nil {
		fmt.Println(err)
		return
	}
	rs := res.GetDataSet()
	//format(rs)
	rs.Close()
	fmt.Println("read use time:", time.Since(start))
	time.Sleep(time.Second)

	_ = op.Close()

}

func testInsertID(host string, port int, timeout int, user string, password string) {

	op, err := opio.Init(host, port, 60, user, password)
	if err != nil {
		fmt.Println(err)
		return
	}

	req := op.NewRequest(nil)
	req.SetID(1)
	req.SetService("openplant")
	req.SetAction(opio.ActionInsert)

	table := opio.NewTable("Realtime", 0)
	table.AddColumn("ID", opio.VtInt32, 0)
	table.AddColumn("AV", opio.VtDouble, 0)
	table.AddColumn("DS", opio.VtInt16, 0)
	table.AddColumn("TM", opio.VtDouble, 0)

	start := time.Now()
	//err = table.SetColumnInt32(0, 174985, 0)
	err = table.SetColumnInt32(0, 7251012, 0)
	if err != nil {
		fmt.Println(err)
		return
	}
	err = table.SetColumnDouble(1, float64(time.Now().Unix()%900))
	if err != nil {
		fmt.Println(err)
		return
	}
	err = table.SetColumnInt16(2, 1, 0)
	if err != nil {
		fmt.Println(err)
		return
	}

	err = table.SetColumnDouble(3, float64(time.Now().Unix()))
	if err != nil {
		fmt.Println(err)
		return
	}
	table.BindRow()

	_ = req.SetTable(table)
	fmt.Println("construct request use time:", time.Since(start))
	start = time.Now()
	err = req.Write()
	if err != nil {
		fmt.Println(err)
		return
	}
	err = req.WriteContent(table)
	if err != nil {
		fmt.Println(err)
		return
	}

	req.Flush()
	fmt.Println("write use time:", time.Since(start))
	start = time.Now()
	res, err := req.GetResponse()
	if err != nil {
		fmt.Println(err)
		return
	}
	rs := res.GetDataSet()
	format(rs)
	rs.Close()
	fmt.Println("read use time:", time.Since(start))
	time.Sleep(time.Second)

	_ = op.Close()

}
func testFind(host string, port int, timeout int, user string, password string) {
	op, err := opio.Init(host, port, 60, user, password)
	if err != nil {
		log.Fatal("init conn error:", err)
		return
	}
	req := op.NewRequest(nil)
	req.SetID(1)
	req.SetService("openplant")
	req.SetAction(opio.ActionSelect)
	table := opio.NewTable("Point", 0)
	//table.AddColumn("*", opio.VtNull, 0)
	table.AddColumn("EC", opio.VtInt8, 0)
	table.AddColumn("ID", opio.VtInt64, 0)
	table.AddColumn("GN", opio.VtString, 0)
	table.AddColumn("EX", opio.VtString, 0)
	table.AddColumn("ED", opio.VtString, 0)
	table.AddColumn("ND", opio.VtInt32, 0)
	table.AddColumn("CP", opio.VtInt32, 0)
	table.AddColumn("PT", opio.VtInt32, 0)
	table.AddColumn("RT", opio.VtInt32, 0)

	//table.AddColumn("TM", opio.VtDateTime, 0)
	//table.AddColumn("DS", opio.VtInt16, 0)
	//table.AddColumn("AV", opio.VtObject, 0)
	_ = req.SetTable(table)
	//count := 1000
	//ids := make([]int32, count)
	//for i := 0; i < count; i++ {
	//	ids[i] = int32(i + 2106176)
	//}
	//req.SetIndexesInt32("ID", ids)

	now := time.Now()

	_ = req.WriteAndFlush()
	res, err := req.GetResponse()
	fmt.Println(err)
	rs := res.GetDataSet()
	format(rs)
	rs.Close()
	fmt.Println(time.Since(now))
	_ = op.Close()
}

func testFindReal(host string, port int, timeout int, user string, password string) {
	op, err := opio.Init(host, port, 60, user, password)
	if err != nil {
		log.Fatal("init conn error:", err)
		return
	}
	req := op.NewRequest(nil)
	req.SetID(1)
	req.SetService("openplant")
	req.SetAction(opio.ActionSelect)
	table := opio.NewTable("Realtime", 0)
	//table.AddColumn("*", opio.VtNull, 0)
	table.AddColumn("ID", opio.VtInt64, 0)
	table.AddColumn("RT", opio.VtInt32, 0)
	table.AddColumn("DS", opio.VtInt32, 0)
	table.AddColumn("AV", opio.VtInt32, 0)
	table.AddColumn("TM", opio.VtInt32, 0)

	//table.AddColumn("TM", opio.VtDateTime, 0)
	//table.AddColumn("DS", opio.VtInt16, 0)
	//table.AddColumn("AV", opio.VtObject, 0)
	_ = req.SetTable(table)
	count := 20
	ids := make([]int32, count)
	for i := 0; i < count; i++ {
		ids[i] = int32(174985)
	}
	req.SetIndexesInt32("ID", ids)

	now := time.Now()

	_ = req.WriteAndFlush()
	res, err := req.GetResponse()
	fmt.Println(err)
	rs := res.GetDataSet()
	format(rs)
	rs.Close()
	fmt.Println(time.Since(now))
	_ = op.Close()
}

func main() {
	host := "11.10.0.5"
	host = "10.202.10.151"
	port := 8200
	timeout := 60
	user := "sis"
	password := "openplant"
	//testFindReal(host, port, timeout, user, password)
	//testSub(host, port, timeout, user, password)
	//testInsertSQL(host, port, timeout, user, password)
	testInsertID(host, port, timeout, user, password)
}

func format(rs *opio.OPDataSet) {
	fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~~")
	count := 0
	for {
		ok, err := rs.Next()
		if ok && err == nil {
			for i, col := range rs.GetColumns() {
				value, _ := rs.GetValue(uint32(i))
				fmt.Print(col.GetName(), ":", value, "\t")
			}
			fmt.Println()
			count++
		} else {
			break
		}
	}
	fmt.Println("resultset row count ", count)
}
